﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Cms.Render;
using System.Collections.ObjectModel;
using System.Collections;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Cms.Bussiness;
using Portal.Core;
using Portal.Cms.Utility;

namespace Portal.Cms.Render
{
    public class DataRenderByTreeBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
                if (string.IsNullOrEmpty(GroupDataFolderId)) GroupDataFolderId = "";
                string Html =
                    DrawFunction(SiteId, gid).HtmlContent +
                    "<script language=javascript>\r\n"+
                    "   function ViewDocument(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       window.open('http://localhost:58/pdf/web/viewer.aspx?gid='+GroupDataId+'&id='+DataId,'_blank');\r\n" +
                    "   }\r\n"+
                    
                    "   function RemoveDocument(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                    "   {\r\n"+
                    "       if(confirm('Bạn đã chắc chắn loại bỏ văn bản khỏi thư mục này chưa?')==false)return;\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideRemoveDocumentFromFolder(SiteId,GroupDataId,GroupDataFolderId,DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n"+
                    "   }\r\n"+
                    
                    "   function UpdateDocument(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       window.open('UpdateDocument.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId+'&DataId='+DataId,'_self');\r\n" +
                    "   }\r\n"+
                    "</script>\r\n"+
                    "<div class=CssFormData>\r\n" +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1><div class=CssDivFolder>" + DrawFolder(SiteId,gid).HtmlContent + "</div></td>\r\n" +
                    "       <td class=col2>\r\n"+
                    "           <h3>Danh sách văn bản trong thư mục<input type=button class=button style=\"float:right;margin-right:2px;\" value=\"Điều kiện tìm kiếm\"></h3>\r\n"+
                    "           <div class=CssDocumentList id=\"divDocumentList\">" +
                                       ServerSideReadingDocumentInFolder(SiteId, gid, GroupDataFolderId).HtmlContent + "\r\n" +
                    "           </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut DrawDocumentList(string SiteId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "<h3>Danh sách văn bản trong thư mục<input type=button class=button style=\"float:right;margin-right:2px;\" value=\"Điều kiện tìm kiếm\"></h3>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFolder(string SiteId,string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html =
                              "<script language=javascript>\r\n"+
                              "     function ClientLoadDocument(GroupDataId,GroupDataFolderId)\r\n"+
                              "     {\r\n"+
                              "         SiteId='"+SiteId+"';\r\n"+
                              "         document.getElementById('txtSelectedGroupDataFolderId').value = GroupDataFolderId;\r\n" +
                              "         AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideReadingDocumentInFolder(SiteId,GroupDataId,GroupDataFolderId).value;\r\n"+
                              "         document.getElementById('divDocumentList').innerHTML=AjaxOut.HtmlContent;\r\n"+
                              "     }\r\n"+
                              "</script>\r\n"+
                              "<input type=hidden id=\"txtSelectedGroupDataFolderId\" value=\"\">\r\n" +
                              "<h3><a href=\"DocumentInputDataTreeView.aspx?gid="+GroupDataId+"\">Thư mục dữ liệu</a></h3>\r\n" +
                              "<div id=\"tree\">\r\n"+
                              "<ul>\r\n";
                GroupDataFolderCls[] GroupDataFolders=GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId,GroupDataId,null);
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + GroupDataId + "|" + GroupDataFolders[iIndex].GroupDataFolderId + "\" target=\"_self\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                BuildSubTreeView(SiteId,GroupDataId,GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent+
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId, string GroupDataId,string ParentGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, ParentGroupDataFolderId);
                Html+=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupDataId + "|" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFunction(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html =
                   "<script language=javascript>\r\n"+
                   "    function ClientAdd(GroupDataId)\r\n"+
                   "    {\r\n"+
                   "        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n"+
                   "        window.open('AddFolderData.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId,'_self');\r\n" +
                   "    }\r\n"+

                   "    function ClientAddDocumentData(GroupDataId)\r\n" +
                   "    {\r\n"+
                   "        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n"+
                   "        if(GroupDataFolderId=='')\r\n"+
                   "        {\r\n"+
                   "            alert('Chưa xác định thư mục để thêm mới tài liệu!');\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        window.open('AddDocumentData.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId,'_self');\r\n" +
                   "    }\r\n"+

                   "    function ClientCollection(GroupDataId)\r\n" +
                   "    {\r\n"+
                   "        window.open('Collection.aspx?gid='+GroupDataId,'_self');\r\n" +
                   "    }\r\n"+

                   "    function ClientDeleteFolder(GroupDataId)\r\n" +
                   "    {\r\n"+
                   "        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n" +
                   "        if(GroupDataFolderId=='')\r\n" +
                   "        {\r\n" +
                   "            alert('Chưa xác định thư mục để xóa!');\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "       if(confirm('Toàn bộ các liên kết với văn bản trong thư mục sẽ bị xóa. Bạn đã chắc chắn xóa thư mục chưa?')==false)return;\r\n" +
                   "       OAjaxOut=Portal.Cms.Render.DataRenderByTreeBll.ServerSideDeleteFolder(SiteId,GroupDataId,GroupDataFolderId).value;\r\n" +
                   "       if(OAjaxOut.Error)\r\n" +
                   "       {\r\n" +
                   "           alert(OAjaxOut.InfoMessage);\r\n" +
                   "           return;\r\n" +
                   "       }\r\n" +
                   "       window.open(OAjaxOut.RetUrl,'_self');\r\n"+
                   "    }\r\n"+

                   //"    function ClientPermissionFolder(GroupDataId)\r\n" +
                   //"    {\r\n" +
                   //"        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n" +
                   //"        if(GroupDataFolderId=='')\r\n" +
                   //"        {\r\n" +
                   //"            alert('Chưa xác định thư mục để phân quyền!');\r\n" +
                   //"            return;\r\n" +
                   //"        }\r\n" +
                   //"       window.open('Permission.aspx?ptc="+DocumentFolderPermission.StaticId+"&id='+GroupDataFolderId,'_self');\r\n" +
                   //"    }\r\n" +

                   "    function ClientUpdateFolder(GroupDataId)\r\n" +
                   "    {\r\n"+
                   "        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n" +
                   "        window.open('UpdateFolderData.aspx?gid='+GroupDataId+'&pgid='+GroupDataFolderId,'_self');\r\n" +
                   "    }\r\n"+

                   "    function ClientMoveFolder(GroupDataId)\r\n" +
                   "    {\r\n"+
                   "        GroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n" +
                   "        window.open('MoveFolder.aspx?gid='+GroupDataId+'&pgid='+GroupDataFolderId,'_self');\r\n" +
                   "    }\r\n"+

                   "</script>\r\n"+
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd('"+GroupDataId+"');\"><img class=image src=\"Resources/Images/AddFolder.png\"><div>Tạo thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientDeleteFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/Delete.png\"><div>Xóa thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientUpdateFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/Update.png\"><div>Sửa thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientMoveFolder('"+GroupDataId+"');\"><img class=image src=\"Resources/Images/MoveFolder.png\"><div>Chuyển thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientAddDocumentData('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/AddDocument.png\"><div>Thêm tài liệu</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientCollection('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/Collection.png\"><div>Dữ liệu thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('home.aspx','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddFolder(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
            string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
            if (string.IsNullOrEmpty(GroupDataFolderId)) GroupDataFolderId = "";
            try
            {
                string Html =
                    "<script language=javascript>\r\n"+
                    "   function ClientAddFolder(GroupDataId)\r\n"+
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       ParentGroupDataFolderId=document.getElementById('txtParentGroupDataFolderId').value;\r\n" +
                    "       FolderName = document.getElementById('txtFolderName').value;\r\n"+
                    "       FolderDescription = document.getElementById('txtFolderDescription').value;\r\n" +
                    "       SortIndex= document.getElementById('txtSortIndex').value;\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.DataRenderByTreeBll.ServerSideAddFolder(SiteId,GroupDataId,FolderName,FolderDescription, ParentGroupDataFolderId, SortIndex).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(OAjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('txtFolderName').value='';\r\n" +
                    "       document.getElementById('txtFolderDescription').value='';\r\n" +
                    "       document.getElementById('txtSortIndex').value=parseInt(SortIndex)+1;\r\n" +
                    "       document.getElementById('txtFolderName').focus();\r\n"+
                    "   }\r\n"+
                    "</script>\r\n"+
                    "<input type=hidden id=txtParentGroupDataFolderId value=\"" + GroupDataFolderId + "\">\r\n" +
                    "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/AddFolder.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid="+GroupDataId+"','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n"+

                    "<div class=CssTip>Cho phép người quản lý tạo ra các thư mục dữ liệu. Các thư mục này sẽ chứa các văn bản tài liệu phục vụ quá trình tạo các thư mục cho người dùng</div>\r\n" +
                    "<div class=CssFormData>\r\n" +
                    "   <table class=tabMain>\r\n" +
                    "       <tr>\r\n" +
                    "           <td><img src=\"Resources/Images/AddFolderLarge.png\"></td>\r\n" +
                    "           <td>\r\n" +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Tên thư mục:</td>\r\n" +
                    "       <td class=col2><input class=textbox type=textbox id=txtFolderName></td>\r\n" +
                    "   </tr>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Mô tả:</td>\r\n" +
                    "       <td class=col2><textarea id=txtFolderDescription type=textarea class=textarea></textarea></td>\r\n" +
                    "   </tr>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Thứ tự:</td>\r\n" +
                    "       <td class=col2><input class=textbox type=textbox id=txtSortIndex value=\"0\"></td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</td>\r\n" +
                    "</tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddFolder(string SiteId, string GroupDataId, string FolderName, string Description, string ParentGroupDataFolderId, string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataFolderProcessBll.AddGroupDataFolder(SiteId, GroupDataId, FolderName, Description, ParentGroupDataFolderId, SortIndex);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideReadingDocumentInFolder(
        //    string SiteId, 
        //    string GroupDataId, 
        //    string GroupDataFolderId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "";
        //        GroupDataFolderCls OGroupDataFolder = null;
        //        if (!string.IsNullOrEmpty(GroupDataFolderId))
        //        {
        //            OGroupDataFolder = GroupDataFolderProcessBll.CreateGroupDataFolderModel(SiteId, GroupDataFolderId);
        //        }
                
        //        if (OGroupDataFolder != null)
        //        {
        //            Html +=
        //                "<h3>Đang chọn thư mục: " + OGroupDataFolder.FolderName + "</h3>";
        //        }
        //        CmsDataCls[] CmsDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataForFolder(SiteId, GroupDataId, GroupDataFolderId);
        //        if (CmsDatas.Length == 0)
        //        {
        //            Html += "<div class=CssNoData>Không có văn bản nào thuộc thư mục</div>\r\n";
        //        }
        //        else
        //        {
        //            Html += "<div class=tabcontent id=divListDocResult>\r\n" +
        //                   "    <table>\r\n" +
        //                   "        <tr>\r\n" +
        //                   "            <th class=thIndex>#</th>\r\n" +
        //                   "            <th>Tiêu đề văn bản</th>\r\n" +
        //                   "            <th>Kiểu</th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "        </tr>\r\n";
        //            for (int iIndex = 0; iIndex < CmsDatas.Length; iIndex++)
        //            {
        //                Html+=
        //                   "        <tr>\r\n" +
        //                   "            <td class=tdIndex>"+(iIndex+1)+"</td>\r\n" +
        //                   "            <td><span class=CssDocumentSubject><a href=\"ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId=" + CmsDatas[iIndex].DataId + "\">" + CmsDatas[iIndex].DataAlias+" / "+ CmsDatas[iIndex].Subject + "</a></span><br><span class=CssDocDesc>" + FunctionUtilities.CutText(CmsDatas[iIndex].Description, 50) + "</span></td>\r\n" +
        //                   "            <td>" + CmsDatas[iIndex].SubDataType + "</td>\r\n" +
        //                   "            <td class=tdButton><a href=\"javascript:RemoveDocument('" + GroupDataId + "','" + GroupDataFolderId + "','" + CmsDatas[iIndex].DataId + "');\"><img alt=\"Loại khỏi thư mục\" title=\"Loại khỏi thư mục\" class=imagebutton src=\"Resources/Images/RemoveFromFolder.png\"></a></td>\r\n" +
        //                   "            <td class=tdButton><a href=\"javascript:UpdateDocument('"+GroupDataId+"','"+GroupDataFolderId+"','"+CmsDatas[iIndex].DataId+"');\"><img alt=\"Sửa thuộc tính tài liệu\" title=\"Sửa thuộc tính tài liệu\" class=imagebutton src=\"Resources/Images/Update.png\"></a></td>\r\n" +
        //                   "            <td class=tdButton><a href=\"javascript:ViewDocument('" + GroupDataId + "','" + GroupDataFolderId + "','" + CmsDatas[iIndex].DataId + "');\"><img alt=\"Xem tài liệu toàn văn\" title=\"Xem tài liệu toàn văn\" class=imagebutton src=\"Resources/Images/Download.png\"></a></td>\r\n" +
        //                   "            <td class=tdButton><a href=\"ChangeAttachDocFile.aspx?gid="+GroupDataId+"&id="+CmsDatas[iIndex].DataId+"\"><img alt=\"Thay đổi lại tệp toàn văn\" title=\"Thay đổi lại tệp toàn văn\" class=imagebutton src=\"Resources/Images/UploadButton.png\"></a></td>\r\n" +
        //                   "            <td class=tdButton><a href=\"AddDataToFolder.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId=" + CmsDatas[iIndex].DataId+ "\"><img alt=\"Phân vào thư mục\" title=\"Phân vào thư mục\" class=imagebutton src=\"Resources/Images/Category.png\"></a></td>\r\n" +
        //                   "        </tr>\r\n";
        //            }
        //            Html += "</table>\r\n" +
        //                    "</div>\r\n";
        //        }
        //        OAjaxOut.HtmlContent = Html;
                    
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddDocumentData(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
            string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
            if (string.IsNullOrEmpty(GroupDataFolderId)) GroupDataFolderId = "";
            string NewDataId = System.Guid.NewGuid().ToString();
            string UploadHandler = "CmsUploadAttachFileHandler.ashx";
            try
            {
                string Html =
                    "<script language=javascript>\r\n" +
                    "  function CallSaveAddData()\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      GroupDataId='" + GroupDataId + "';\r\n" +
                     "      DataId=document.getElementById('txtDataId').value;\r\n" +
                     "      Alias = document.getElementById('txtAlias').value;\r\n" +
                     "      Subject = document.getElementById('txtSubject').value;\r\n" +
                     "      Description = document.getElementById('txtDescription').value;\r\n" +
                     "      Body = '';\r\n" +
                     "      if(FCKeditorAPI!=null){\r\n" +
                     "          oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                     "          Body= oEditor.GetXHTML( true ); \r\n" +
                     "      }\r\n" +
                     "      Source = document.getElementById('txtSource').value;\r\n" +
                     "      Url = document.getElementById('txtUrl').value;\r\n" +
                     "      Target = document.getElementById('txtTarget').value;\r\n" +
                     "      UploadFile=document.getElementById('txtUploadFile').value;\r\n" +
                     "      SortIndex=document.getElementById('txtSortIndex').value;\r\n" +
                     "      ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +

                     "      DocType='';\r\n" +
                     "      if(document.getElementById('drpSelectDocType')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocType=document.getElementById('drpSelectDocType').value;\r\n" +
                     "      }\r\n" +

                     "      DocField='';\r\n" +
                     "      if(document.getElementById('drpSelectDocField')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocField=document.getElementById('drpSelectDocField').value;\r\n" +
                     "      }\r\n" +

                     "      DocReleaseFrom='';\r\n" +
                     "      if(document.getElementById('drpSelectDocReleaseFrom')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocReleaseFrom=document.getElementById('drpSelectDocReleaseFrom').value;\r\n" +
                     "      }\r\n" +
                     "      DocSigner='';\r\n" +
                     "      if(document.getElementById('drpSelectDocSigner')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocSigner=document.getElementById('drpSelectDocSigner').value;\r\n" +
                     "      }\r\n" +

                     "      DocStatus='';\r\n" +
                     "      if(document.getElementById('drpSelectDocStatus')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocStatus=document.getElementById('drpSelectDocStatus').value;\r\n" +
                     "      }\r\n" +

                     "      ReleaseDate='';\r\n" +
                     "      if(document.getElementById('txtReleaseDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReleaseDate=document.getElementById('txtReleaseDate').value;\r\n" +
                     "      }\r\n" +

                     "      EffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EffectedDate=document.getElementById('txtEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      EndEffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEndEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EndEffectedDate=document.getElementById('txtEndEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      SignDate='';\r\n" +
                     "      if(document.getElementById('txtSignDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          SignDate=document.getElementById('txtSignDate').value;\r\n" +
                     "      }\r\n" +

                     "      ReplaceDoc='';\r\n" +
                     "      if(document.getElementById('txtReplaceDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReplaceDoc=document.getElementById('txtReplaceDoc').value;\r\n" +
                     "      }\r\n" +

                     "      ModifyDoc='';\r\n" +
                     "      if(document.getElementById('txtModifyDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ModifyDoc=document.getElementById('txtModifyDoc').value;\r\n" +
                     "      }\r\n" +

                     "      VoteType='';\r\n" +
                     "      if(document.getElementById('drpSelectVoteType')!=null)\r\n" +
                     "      {\r\n" +
                     "          VoteType=document.getElementById('drpSelectVoteType').value;\r\n" +
                     "      }\r\n" +

                     "      IsNew = document.getElementById('drpSelectIsNew').value;\r\n" +
                     "      Searchable=0;\r\n" +
                     "      if(document.getElementById('drpSelectSearchable')!=null)\r\n" +
                     "      {\r\n" +
                     "          Searchable=document.getElementById('drpSelectSearchable').value;\r\n" +
                     "      }\r\n" +
                     "      GroupDataFolderId=document.getElementById('txtGroupDataFolderId').value;\r\n"+
                     "      AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideAdd(SiteId, DataId, GroupDataId, ParentDataId, Alias, Subject, Description, Body, Source, Url, Target, UploadFile, SortIndex, DocType,DocField, DocReleaseFrom,DocSigner,DocStatus,SignDate, ReleaseDate,EffectedDate,EndEffectedDate,ReplaceDoc,ModifyDoc, VoteType, IsNew, Searchable, GroupDataFolderId).value;\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      if(AjaxOut.RetUrl!='')\r\n" +
                     "      {\r\n" +
                     "          window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "      }\r\n" +
                     //"      else\r\n" +
                     //"      {\r\n" +
                     //"          document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                     //"          ClientBack();\r\n" +
                     //"      }\r\n" +
                     "  }\r\n" +


                     "function fileSelected() {\r\n" +
                     "    var file = document.getElementById('fileToUpload').files[0];\r\n" +
                     "    if (file) {\r\n" +
                     "        var fileSize = 0;\r\n" +
                     "        if (file.size > 1024 * 1024)\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                     "        else\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                     "        document.getElementById('fileName').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                     "        document.getElementById('fileSize').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                     "        document.getElementById('fileType').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     "function uploadFile() {\r\n" +
                     "    if(document.getElementById('fileToUpload').value==''){\r\n" +
                     "       CallSaveAddData();\r\n" +
                     "       return;\r\n" +
                     "   }\r\n" +
                     "    DataId=document.getElementById('txtDataId').value;\r\n" +
                     "    var fd = new FormData();\r\n" +
                     "    fd.append(\"fileToUpload\", document.getElementById('fileToUpload').files[0]);\r\n" +
                     "    var xhr = new XMLHttpRequest();\r\n" +
                     "    xhr.upload.addEventListener(\"progress\", uploadProgress, false);\r\n" +
                     "    xhr.addEventListener(\"load\", uploadComplete, false);\r\n" +
                     "    xhr.addEventListener(\"error\", uploadFailed, false);\r\n" +
                     "    xhr.addEventListener(\"abort\", uploadCanceled, false);\r\n" +
                     "    xhr.open(\"POST\", \"" + UploadHandler + "?sid=" + SiteId + "&uni=" + WebSessionUtility.GetCurrentLoginUser().AssetCode + "&gid=" + GroupDataId + "&dataid=" + NewDataId + "\");\r\n" +
                     "    xhr.send(fd);\r\n" +
                     "}\r\n" +

                     "function uploadProgress(evt) {\r\n" +
                     "    if (evt.lengthComputable) {\r\n" +
                     "    }\r\n" +
                     "    else {\r\n" +
                     "        document.getElementById('progressNumber').innerHTML = 'Không tính toán được thời gian';\r\n" +
                     "    }\r\n" +
                     "}\r\n" +



                     " function uploadComplete(evt) {\r\n" +
                     "    /* This event is raised when the server send back a response */\r\n" +
                     "  if(evt.target.responseText.indexOf('ERROR')!=-1)\r\n" +
                     "  {\r\n" +
                     "      alert(evt.target.responseText.substring(6));\r\n" +
                     "      return;\r\n" +
                     "  }\r\n" +
                     "    document.getElementById('txtUploadFile').value=evt.target.responseText;\r\n" +
                     "    //doan nay chay upload vao day...\r\n" +
                     "    CallSaveAddData();\r\n" +
                     "   }\r\n" +

                       "function uploadFailed(evt) {\r\n" +
                     "       alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                     "   }\r\n" +

                     "   function uploadCanceled(evt) {\r\n" +
                     "        alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                     "   }\r\n" +


                    "</script>\r\n" +
                    "<input type=hidden id=txtGroupDataFolderId value=\"" + GroupDataFolderId + "\">\r\n" +
                    "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:uploadFile();\"><img class=image src=\"Resources/Images/Upload.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid=" + GroupDataId + "','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";


                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);
                string StyleDisp = " style=\"display:none;\"";
                if (OGroupData.ShowBody)
                {
                    StyleDisp = "";
                }

                
                string CodeCaption = "Mã";
                string DescCaption = "Mô tả";
                string AttachCaption = "Ảnh gắn kèm";
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    CodeCaption = "Số văn bản";
                    DescCaption = "Mô tả / trích yếu";
                    AttachCaption = "Tệp toàn văn";
                }


                string SelectIsNew = " <select id=\"drpSelectIsNew\">\r\n" +
                     "      <option value=\"0\">Tài liệu bình thường</option>\r\n" +
                     "      <option value=\"1\" selected>Tài liệu mới</option>\r\n" +
                     " </select>\r\n";


                string SelectSearchable =
                    " <select id=\"drpSelectSearchable\">\r\n" +
                    "      <option value=\"0\">Không tìm kiếm toàn văn được</option>\r\n" +
                    "      <option value=\"1\" selected>Tìm kiếm toàn văn được</option>\r\n" +
                    " </select>\r\n";

                Html +=
                   "  <input type=hidden id= txtParentDataId value=\"\">\r\n" +
                   "  <input type=hidden id=txtDataId value=\"" + NewDataId + "\">\r\n" +
                   "  <h3>Thêm mới dữ liệu</h3>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n"+
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>" + CodeCaption + ":</td>\r\n" +
                   "        <td class=c2><input id=txtAlias value=\"" + System.Guid.NewGuid().ToString().Substring(0, 8) + "\" class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Tiêu đề:</td>\r\n" +
                   "        <td class=c2><input id=txtSubject class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1 style=\"vertical-align:top;\">" + DescCaption + ":</td>\r\n" +
                   "        <td class=c2><textarea id=txtDescription class=textarea type=textbox></textarea></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr " + StyleDisp + ">\r\n" +
                   "        <td class=c1 style=\"vertical-align:top;\">Nội dung:</td>\r\n" +
                   "        <td class=c2>\r\n" +
                   "                <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\"></textarea>\r\n" +
                    "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                    "               <script type=\"text/javascript\">\r\n" +
                    "                   var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
                    "                   oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
                    "                   oFCKeditor.Height = \"600\";\r\n" +
                    "                   oFCKeditor.ReplaceTextarea();\r\n" +
                    "               </script>\r\n" +
                   "        </td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Nguồn:</td>\r\n" +
                   "        <td class=c2><input id=txtSource class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Url:</td>\r\n" +
                   "        <td class=c2><input id=txtUrl class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Kiểu link:</td>\r\n" +
                   "        <td class=c2><input id=txtTarget class=textbox type=textbox value=\"_self\"></td>\r\n" +
                   "    </tr>\r\n";
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    string SelectDocumentType =
                        "<select id=drpSelectDocType class=drplist>\r\n";
                    DocTypeCls[] DocTypes = DocTypeProcessBll.ReadingDocType(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
                    {
                        SelectDocumentType += "   <option value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                    }
                    SelectDocumentType += "</select>\r\n";


                    string SelectDocField =
                      "<select id=drpSelectDocField class=drplist>\r\n";
                    DocFieldCls[] DocFields = DocFieldProcessBll.ReadingDocField(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
                    {
                        SelectDocField += "   <option value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                    }
                    SelectDocField += "</select>\r\n";

                    string SelectDocReleaseFrom =
                        "<select id=drpSelectDocReleaseFrom class=drplist>\r\n";
                    DocReleaseFromCls[] DocReleaseFroms = DocReleaseFromProcessBll.ReadingDocReleaseFrom(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocReleaseFroms.Length; iIndex++)
                    {
                        SelectDocReleaseFrom += "   <option value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                    }
                    SelectDocReleaseFrom += "</select>\r\n";

                    string SelectDocSigner =
                      "<select id=drpSelectDocSigner class=drplist>\r\n";
                    DocSignerCls[] DocSigners = DocSignerProcessBll.ReadingDocSigner(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocSigners.Length; iIndex++)
                    {
                        SelectDocSigner += "   <option value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                    }
                    SelectDocSigner += "</select>\r\n";

                    string SelectStatus =
                        "<select id=drpSelectDocStatus class=drplist>\r\n" +
                        "   <option value=\"0\">Hết hiệu lực</option>\r\n" +
                        "   <option value=\"1\">Có hiệu lực</option>\r\n" +
                        "</select>\r\n";

                    Html +=
                     "    <tr>\r\n" +
                     "        <td class=c1>Loại văn bản:</td>\r\n" +
                     "        <td class=c2>" + SelectDocumentType + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Nơi ban hành:</td>\r\n" +
                     "        <td class=c2>" + SelectDocReleaseFrom + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Lĩnh vực:</td>\r\n" +
                     "        <td class=c2>" + SelectDocField + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Người ký:</td>\r\n" +
                     "        <td class=c2>" + SelectDocSigner + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Trạng thái:</td>\r\n" +
                     "        <td class=c2>" + SelectStatus + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày ký:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtSignDate") + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày ban hành:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtReleaseDate") + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày hiệu lực:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtEffectedDate") + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày hết hiệu lực:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtEndEffectedDate") + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Tìm kiếm toàn văn:</td>\r\n" +
                     "        <td class=c2>" + SelectSearchable + "</td>\r\n" +
                     "    </tr>\r\n" +

                   "    <tr>\r\n" +
                   "        <td class=c1>Thứ tự:</td>\r\n" +
                   "        <td class=c2><input value=\"0\" id=txtSortIndex class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Trạng thái:</td>\r\n" +
                   "        <td class=c2>" + SelectIsNew + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "         <td class=c1>" + AttachCaption + ":</td>\r\n" +
                   "         <td class=c2><input type=hidden id=\"txtUploadFile\"><input type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\" onchange=\"fileSelected();\" /></td>\r\n" +
                   "    </tr>\r\n" +
                   "       <tr>\r\n" +
                    "           <td></td>\r\n" +
                    "           <td>\r\n" +
                    "               <div id=\"progressNumber\"></div>\r\n" +
                    "               <div id=\"fileName\">\r\n" +
                    "               </div>\r\n" +
                    "               <div id=\"fileSize\">\r\n" +
                    "               </div>\r\n" +
                    "               <div id=\"fileType\">\r\n" +
                    "               </div>\r\n" +
                    "           </td>\r\n" +
                    "       </tr>\r\n" +

                   //"    <tr>\r\n" +
                        //"        <td class=c1></td>\r\n" +
                        //"        <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:uploadFile();\"><input type=button class=button value=\"Quay lại\" onclick=\"" + BackScript + "\"></td>\r\n" +
                        //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";


                    OAjaxOut.HtmlContent = Html;
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId,
            string DataId,
            string GroupDataId,
            string ParentDataId,
            string Alias,
            string Subject,
            string Description,
            string Body,
            string Source,
            string Url,
            string Target,
            string UploadFile,
            string SortIndex,
            string DocType,
            string DocField,
            string DocReleaseFrom,
            string DocSigner,
            string DocStatus,
            string SignDate,
            string ReleaseDate,
            string EffectedDate,
            string EndEffectedDate,
            string ReplaceDoc,
            string ModifyDoc,
            string VoteType,
            int IsNew,
            int AttachedSearchable,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);
                if (string.IsNullOrEmpty(Alias)) Alias = System.Guid.NewGuid().ToString().Substring(0, 8);
                if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề nhóm dữ liệu!");
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    if (FunctionUtilities.checkVnDate(ReleaseDate) == false) throw new Exception("Ngày ban hành không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EffectedDate) == false) throw new Exception("Ngày hiệu lực không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EndEffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EndEffectedDate) == false) throw new Exception("Ngày hết hiệu lực không hợp lệ!");
                }
                DateTime dReleaseDate = new System.DateTime();
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    dReleaseDate = FunctionUtilities.VNDateToDate(ReleaseDate);
                }
                DateTime dEffectedDate = new System.DateTime();
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    dEffectedDate = FunctionUtilities.VNDateToDate(EffectedDate);
                }
                DateTime dEndEffectedDate = new System.DateTime();
                if (!string.IsNullOrEmpty(EndEffectedDate))
                {
                    dEndEffectedDate = FunctionUtilities.VNDateToDate(EndEffectedDate);
                }
                DateTime dSignDate = new System.DateTime();
                if (!string.IsNullOrEmpty(SignDate))
                {
                    dSignDate = FunctionUtilities.VNDateToDate(SignDate);
                }
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddCmsData(SiteId, DataId, GroupDataId, ParentDataId, Alias, Subject, Description, Body, Source, Url, Target, UploadFile, SortIndex, DocType, DocField, DocReleaseFrom, DocSigner, DocStatus, dSignDate, dReleaseDate, dEffectedDate, dEndEffectedDate, ReplaceDoc, ModifyDoc, VoteType, true, IsNew, AttachedSearchable, GroupDataFolderId,"document");
                OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid=" + GroupDataId;
                //if (OGroupData.frkDataTypeId.Equals("document"))
                //{
                //    OAjaxOut.RetUrl = "DocumentInputData.aspx?gid=" + OGroupData.GroupDataId;
                //}
                //else
                //{
                //    OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId, ParentDataId).HtmlContent;
                //}
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteFolder(
             string SiteId,
             string GroupDataId,
             string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataFolderProcessBll.DeleteGroupDataFolder(SiteId, GroupDataFolderId);
                OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid=" + GroupDataId;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

        

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFolderForAddData(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
                string DataId = (string)WebEnvironments.Request(SiteId, "DataId");
                string[] CheckedIds = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDataInFolder(SiteId, DataId);
                string Html =
                              "<script language=javascript>\r\n" +
                              "     function ClientProcessCheckBox(GroupDataId,GroupDataFolderId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         CheckedIds=document.getElementById('txtCheckIds').value;\r\n"+
                              "         blnChecked=document.getElementById('chk'+GroupDataFolderId).checked;\r\n"+
                              "         AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideProcessCheckBox(SiteId,GroupDataId,GroupDataFolderId,CheckedIds,blnChecked).value;\r\n" +
                              "         document.getElementById('txtCheckIds').value=AjaxOut.HtmlContent;\r\n" +
                              "     }\r\n" +

                              "     function ClientAddDataToFolder(GroupDataId)\r\n" +
                              "     {\r\n"+
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         CheckedIds=document.getElementById('txtCheckIds').value;\r\n" +
                              "         DataId=document.getElementById('txtDataId').value;\r\n" +
                              "         AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideAddDataToFolder(SiteId,GroupDataId,CheckedIds,DataId).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n"+
                              "         {\r\n"+
                              "             alert(AjaxOut.InfoMessage);\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         window.open(AjaxOut.RetUrl,'_self');\r\n"+
                              "     }\r\n"+
                              "</script>\r\n" +

                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientAddDataToFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/Save.png\"><div>Ghi lại</div></li>\r\n" +
                              "            <li onclick=\"javascript:window.open('ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId="+DataId+"','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +

                              "<input type=hidden id=\"txtCheckIds\" value=\"\">\r\n" +
                              "<input type=hidden id=\"txtDataId\" value=\""+DataId+"\">\r\n" +
                              "<h3>Tích vào các thư mục dữ liệu</h3>\r\n" +
                              "<div id=\"tree\">\r\n"+
                              "<ul>\r\n";
                GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, null);
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    bool bChecked = CheckDataHasInFolder(CheckedIds, GroupDataFolders[iIndex].GroupDataFolderId);
                    if (string.IsNullOrEmpty(GroupDataFolders[iIndex].FolderName))
                    {
                        GroupDataFolders[iIndex].FolderName = "Không có tên thư mục";
                    }
                    string AppendClass = "class=\"folder expanded\"";
                    if (bChecked)
                    {
                        AppendClass = "class=\"folder selected expanded\"";
                    }
                    Html +=
                        //"<li ><input type=checkbox " + (bChecked ? "CHECKED" : "") + " class=CssCheckBox onchange=\"javascript:ClientProcessCheckBox('" + GroupDataId + "','" + GroupDataFolders[iIndex].GroupDataFolderId + "');\" id=\"chk" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
                        "<li "+AppendClass+" id=" + GroupDataFolders[iIndex].GroupDataFolderId + ">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
                            BuildSubTreeViewForAddData(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId,CheckedIds).HtmlContent +
                        "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFolderForMove(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string MoveGroupDataFolderId = (string)WebEnvironments.Request(SiteId, "pgid");
                //string DataId = (string)WebEnvironments.Request(SiteId, "DataId");
                //string[] CheckedIds = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDataInFolder(SiteId, DataId);
                string Html =
                              "<script language=javascript>\r\n" +
                              "     function ClientMoveFolder(GroupDataId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         SelectedFolderId=document.getElementById('txtCheckIds').value;\r\n" +
                              "         AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideMoveFolder(SiteId, GroupDataId,'" + MoveGroupDataFolderId + "', SelectedFolderId).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n" +
                              "         {\r\n" +
                              "             alert(AjaxOut.InfoMessage);\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         window.open(AjaxOut.RetUrl,'_self');\r\n" +
                              "     }\r\n" +
                              "</script>\r\n" +

                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientMoveFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/Save.png\"><div>Ghi lại</div></li>\r\n" +
                              "            <li onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid=" + GroupDataId + "','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +

                              "<input type=hidden id=\"txtCheckIds\" value=\"\">\r\n" +
                              "<h3>Tích vào các thư mục dữ liệu</h3>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n";
                GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, null);
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    if (string.IsNullOrEmpty(GroupDataFolders[iIndex].FolderName))
                    {
                        GroupDataFolders[iIndex].FolderName = "Không có tên thư mục";
                    }
                    string AppendClass = "class=\"folder expanded\"";
                    if (!MoveGroupDataFolderId.Equals(GroupDataFolders[iIndex].GroupDataFolderId))
                    {
                        Html +=
                            "<li " + AppendClass + " id=" + GroupDataFolders[iIndex].GroupDataFolderId + ">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
                                BuildSubTreeViewForMoveFolder(SiteId, GroupDataId, MoveGroupDataFolderId,GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                            "</li>\r\n";
                    }
                }

                Html += "</ul>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideProcessCheckBox(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId,
            string CheckedIds,
            bool Checked)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (Checked)
                {
                    CheckedIds = CheckedIds + GroupDataFolderId+";";
                }
                else
                {
                    CheckedIds = CheckedIds.Replace(GroupDataFolderId + ";", "");
                }

                CheckedIds = CheckedIds.Trim();
                int iFound = CheckedIds.IndexOf(";;");
                while (iFound != -1)
                {
                    CheckedIds = CheckedIds.Replace(";;", ";");
                    iFound = CheckedIds.IndexOf(";;");
                }
                if (CheckedIds.Length > 1)
                {
                    if (CheckedIds[0] == ';')
                    {
                        CheckedIds = CheckedIds.Substring(1);
                    }
                }
                OAjaxOut.HtmlContent = CheckedIds;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideAddDataToFolder(
        //    string SiteId, 
        //    string GroupDataId, 
        //    string CheckedIds,
        //    string DataId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        CheckedIds = CheckedIds.Replace("DynaTreeNode", "");
        //        string[] Items = CheckedIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //        for (int iIndex = 0; iIndex < Items.Length; iIndex++)
        //        {
        //            int iFound = Items[iIndex].IndexOf(":");
        //            Items[iIndex] = Items[iIndex].Substring(0, iFound);
        //            Items[iIndex] = Items[iIndex].Replace("<", "");
        //            Items[iIndex] = Items[iIndex].Replace(">", "");
        //            Items[iIndex] = Items[iIndex].Trim();
        //        }
        //        CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddDataToGroupDataFolder(SiteId, Items, DataId);
        //        OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid="+GroupDataId;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideMoveFolder(
        //    string SiteId,
        //    string GroupDataId,
        //    string fromFolderId,
        //    string toFolderId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        toFolderId = toFolderId.Replace("DynaTreeNode", "");
        //        int iFound = toFolderId.IndexOf(":");
        //        toFolderId = toFolderId.Substring(0, iFound);
        //        toFolderId = toFolderId.Replace("<", "");
        //        toFolderId = toFolderId.Replace(">", "");
        //        toFolderId = toFolderId.Trim();

        //        CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).MoveFolder(SiteId, fromFolderId,toFolderId);
        //        OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid=" + GroupDataId;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}



        //static bool CheckDataHasInFolder(string[] CheckedIds, string ItemCheckId)
        //{
        //    for (int iIndex = 0; iIndex < CheckedIds.Length; iIndex++)
        //    {
        //        if (CheckedIds[iIndex].Equals(ItemCheckId))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut BuildSubTreeViewForAddData(string SiteId, string GroupDataId, string ParentGroupDataFolderId, string[] CheckedIds)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "";
        //        GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, ParentGroupDataFolderId);
        //        Html +=
        //            "<ul>\r\n";
        //        for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
        //        {
        //            bool bChecked = CheckDataHasInFolder(CheckedIds, GroupDataFolders[iIndex].GroupDataFolderId);
        //            string AppendClass = "class=\"folder\"";
        //            if (bChecked)
        //            {
        //                AppendClass = "class=\"folder selected\"";
        //            }
        //            Html +=
        //                //"<li><input onchange=\"javascript:ClientProcessCheckBox('" + GroupDataId + "','" + GroupDataFolders[iIndex].GroupDataFolderId + "');\" type=checkbox class=CssCheckBox " + (bChecked?"CHECKED":"") + " id=\"chk" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
        //                " <li "+AppendClass+" id=" + GroupDataFolders[iIndex].GroupDataFolderId + ">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
        //                        BuildSubTreeViewForAddData(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId,CheckedIds).HtmlContent +
        //                "</li>\r\n";
        //        }

        //        Html += "</ul>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut BuildSubTreeViewForMoveFolder(string SiteId, string GroupDataId,string MoveGroupDataFolderId, string ParentGroupDataFolderId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "";
        //        GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, ParentGroupDataFolderId);
        //        Html +=
        //            "<ul>\r\n";
        //        for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
        //        {
        //            if (!MoveGroupDataFolderId.Equals(GroupDataFolders[iIndex].GroupDataFolderId))
        //            {
        //                string AppendClass = "class=\"folder\"";
        //                Html +=
        //                    " <li " + AppendClass + " id=" + GroupDataFolders[iIndex].GroupDataFolderId + ">" + GroupDataFolders[iIndex].FolderName + "\r\n" +
        //                            BuildSubTreeViewForMoveFolder(SiteId, GroupDataId, MoveGroupDataFolderId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
        //                    "</li>\r\n";
        //            }
        //        }

        //        Html += "</ul>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


        public static AjaxOut ViewDocumentDetail(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Id = (string)WebEnvironments.Request(SiteId, "DataId");
                string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
                //string BackAspx = "ViewDocument.aspx";
                if (string.IsNullOrEmpty(Id)) throw new Exception("Tài liệu không tìm thấy! Truy vấn không hợp lệ");
                
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(SiteId, Id, true,false);
                if (OCmsData == null) throw new Exception("Tài liệu không tìm thấy! Truy vấn không hợp lệ");
                string gid = OCmsData.frkGroupDataId;
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrl = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                string PreviewImage = ButtonUrl+"/cms/view_detailed.png";

                string NewImageText = "";
                if (OCmsData.IsNew == 1)
                {
                    NewImageText = "<img class=imgthumb src=\""+ButtonUrl+"/cms/Newred.png\"></img>";
                }
                string ShowRelChanged = (string)WebEnvironments.Request(SiteId, "ShowRelChanged");
                if (string.IsNullOrEmpty(ShowRelChanged)) ShowRelChanged = "";
                string Html =
                    " <script language=javascript>\r\n" +
                    "   function ClientDownloadAttachFile(DataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideDownloadAttachFile(SiteId,DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_blank');\r\n" +
                    "   }\r\n" +

                    "   function ClientAddRelationDocument(GroupDataId,DataId,RelationTypeId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       documentNo=document.getElementById('txtRelationTypeText'+RelationTypeId).value;\r\n" +
                    "       documentNo=documentNo.trim();\r\n" +
                    "       if(documentNo=='')\r\n" +
                    "       {\r\n" +
                    "           alert('Chưa nhập số văn bản liên quan!');\r\n" +
                    "           document.getElementById('txtRelationTypeText'+RelationTypeId).focus();\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideAddRelationDocument(SiteId,GroupDataId, DataId, RelationTypeId, documentNo, '" + GroupDataFolderId + "').value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientDeleteRelation(RelationDataId,GroupDataId,DataId,RelationTypeId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideDeleteRelationDocument(SiteId,RelationDataId, GroupDataId, DataId, RelationTypeId, '" + GroupDataFolderId + "').value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientUpdateData(GroupDataId,GroupDataFolderId,DataId)\r\n"+
                    "   {\r\n"+
                    "       window.open('UpdateDocument.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId+'&DataId='+DataId,'_self');\r\n"+
                    "   }\r\n"+

                    "   function ClientAdd2Category(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                    "   {\r\n" +
                    "       window.open('AddDataToFolder.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId+'&DataId='+DataId,'_self');\r\n" +
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideDeleteDocument(SiteId,RelationDataId, GroupDataId, DataId, RelationTypeId, '" + GroupDataFolderId + "').value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientDelete(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       if(confirm('Bạn đã chắc chắn xóa văn bản và toàn bộ liên kết chưa?')==false)return;\r\n"+
                    "       AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideDeleteDocument(SiteId,GroupDataId,GroupDataFolderId,DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n"+

                    " </script>\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientDelete('" + gid + "','" + GroupDataFolderId + "','" + OCmsData.DataId + "');\"><img class=image src=\"Resources/Images/Delete.png\"><div>Xoá văn bản</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientUpdateData('"+gid+"','"+GroupDataFolderId+"','"+OCmsData.DataId+"');\"><img class=image src=\"Resources/Images/Update.png\"><div>Sửa văn bản</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd2Category('" + gid + "','" + GroupDataFolderId + "','" + OCmsData.DataId + "');\"><img class=image src=\"Resources/Images/Category.png\"><div>Thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid="+gid+"&GroupDataFolderId="+GroupDataFolderId+"','_self');\"><img class=image src=\"Resources/Images/Back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +

                    " <div class=CssListCategory> \r\n" +
                    " <h3>" + OCmsData.Subject + "</h3>\r\n" +
                    " <table> \r\n" +
                    "     <tr> \r\n" +
                    "         <td style=\"width:40px;vertical-align:top\">" + NewImageText + "</td> \r\n" +
                    "         <td> \r\n" +
                    "             <div class=viewdetail> \r\n" +
                    "             <table> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Số văn bản:</td> \r\n" +
                    "                     <td class=col2><span class=DocNo>" + OCmsData.DataAlias + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Tiêu đề văn bản:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.Subject + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Lĩnh vực:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocFieldName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Loại văn bản:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocTypeName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Đơn vị ban hành:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocReleaseFromName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Ngày ký:</td> \r\n" +
                    "                     <td class=col2><span class=CssDate>" + OCmsData.SignDate + "</span</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Ngày ban hành:</td> \r\n" +
                    "                     <td class=col2><span class=CssDate>" + OCmsData.ReleaseDate + "</span</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Ngày hiệu lực:</td> \r\n" +
                    "                     <td class=col2><span class=CssDate>" + OCmsData.EffectedDate + "</span</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Trích yếu:</td> \r\n" +
                    "                     <td class=col2><span class=CssTrichYeuDetail>" + OCmsData.Description + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1></td> \r\n" +
                    "                     <td class=col2>Số lần xem: <span class=CssTotalView>" + OCmsData.TotalPreview.ToString("#,##0") + "</span> | Số lần tải: <span class=CssTotalDownload>" + OCmsData.TotalDownload.ToString("#,##0") + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Xem / Tải về máy:</td> \r\n" +
                    "                     <td class=col2><div class=download><img onclick=\"javascript:window.open('ViewDigitalFile.aspx?DataId=" + OCmsData.DataId + "','_blank');\" style=\"border:none;\" alt=\"Xem tài liệu toàn văn\" title=\"Xem tài liệu toàn văn\" class=\"image\" src=\"" + PreviewImage + "\"><img onclick=\"javascript:ClientDownloadAttachFile('" + OCmsData.DataId + "');\" style=\"border:none;\" alt=\"Download tài liệu gắn kèm\" title=\"Download tài liệu gắn kèm\" class=image src=\"Resources/Images/download.png\"></div></td> \r\n" +
                    "                 </tr> \r\n" +
                    "             </table> \r\n" +
                    "             </div> \r\n" +
                    "         </td> \r\n" +
                    "     </tr> \r\n" +
                    "</table> \r\n";

                //GroupDataFolderCls[] GroupDataFolders = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataFolderForData(SiteId, Id);
                //if (GroupDataFolders.Length > 0)
                //{
                //    Html += "<div class=\"CssBelongCategory\">\r\n" +
                //            "<h3>Thuộc chuyên mục</h3>\r\n" +
                //            "<div class=CssBelongData>\r\n";
                            
                //    for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                //    {
                //        Html += "<span class=CssDataFolder title=\"" + GroupDataFolders[iIndex].Description+ "\">" + GroupDataFolders[iIndex].FolderName + "</span>; ";
                //    }

                //    Html+=
                //        "</div>\r\n"+
                //        "</div>";
                //}

                RelationTypeCls[] RelationTypes = RelationTypeProcessBll.ReadingRelationType(SiteId, OCmsData.frkGroupDataId);
                for (int iIndex = 0; iIndex < RelationTypes.Length; iIndex++)
                {
                    CmsDataRelationCls[] CmsDataRelations = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDocumentRelation(SiteId, OCmsData.DataId, RelationTypes[iIndex].RelationTypeId);
                    Html +=
                    "<div class=CssRelationItem>\r\n"+
                    " <h3>" + RelationTypes[iIndex].RelationTypeName + "</h3>\r\n" +
                    " <div class=CssRelationDivFunc>Số văn bản: <input type=textbox id=\"txtRelationTypeText" + RelationTypes[iIndex].RelationTypeId + "\" class=textbox><input type=button class=button value=\"Đưa vào\" onclick=\"javascript:ClientAddRelationDocument('" + OCmsData.frkGroupDataId + "','" + OCmsData.DataId + "','" + RelationTypes[iIndex].RelationTypeId + "');\"></div>\r\n" +
                    " <div class=relationdoc>\r\n" +
                    " <ul>\r\n";
                    for (int zIndex = 0; zIndex < CmsDataRelations.Length; zIndex++)
                    {
                        Html +=
                            "<li>\r\n" +
                            "   <img class=\"imgdelete\" onclick=\"javascript:ClientDeleteRelation('" + CmsDataRelations[zIndex].RelationDataId + "','" + OCmsData.frkGroupDataId + "','" + OCmsData.DataId + "','" + RelationTypes[iIndex].RelationTypeId + "');\" src=\"resources/images/delete.png\"> Số: <span class=DocNo>" + CmsDataRelations[zIndex].DataAlias + "</span> - <a href=\"?gid=" + gid + "&DataId=" + CmsDataRelations[zIndex].DataId + "&GroupDataFolderId=" + GroupDataFolderId + "\">" + CmsDataRelations[zIndex].Subject + "</a>\r\n" +
                            "</li>\r\n";
                    }
                    Html +=
                    "</ul>\r\n" +
                    "</div>\r\n" +
                    "</div>\r\n";
                }


                RelationTypes = RelationTypeProcessBll.ReadingRelationTypeOut(SiteId, OCmsData.frkGroupDataId);
                for (int iIndex = 0; iIndex < RelationTypes.Length; iIndex++)
                {
                    CmsDataRelationCls[] CmsDataRelations = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDocumentRelationOut(SiteId, OCmsData.DataId, RelationTypes[iIndex].RelationTypeId);
                    if (CmsDataRelations.Length > 0)
                    {
                        Html +=
                        " <h3>Bị " + RelationTypes[iIndex].RelationTypeName.ToLower() + " bởi</h3>\r\n" +
                        " <div class=relationdoc>\r\n" +
                        " <ul>\r\n";
                        for (int zIndex = 0; zIndex < CmsDataRelations.Length; zIndex++)
                        {
                            Html +=
                                "<li>\r\n" +
                                "    <span class=DocNo>Số: " + CmsDataRelations[zIndex].DataAlias + " - <a href=\"?gid="+gid+"&DataId=" + CmsDataRelations[zIndex].DataId + "&GroupDataFolderId=" + GroupDataFolderId + "\">" + CmsDataRelations[zIndex].Subject + "</a></span>\r\n" +
                                "</li>\r\n";
                        }
                        Html +=
                        "</ul>\r\n" +
                        "</div>\r\n";
                    }
                }




                Html +=
                   " <div class=\"clr\"></div>\r\n";
                   
                if (ShowRelChanged.Equals("1"))
                {
                    //doan nay show cac van ban lien quan bi thay doi
                    Collection<CmsDataCls> ResCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).GetDocumentRelation(SiteId, Id, null);
                    ResCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).GetDocumentRelationRevert(SiteId, Id, ResCmsData);
                    if (ResCmsData.Count > 0)
                    {
                        Hashtable hs = new Hashtable();
                        Html += 
                                "<div class=CssRelationDocHasEffected>\r\n"+
                                "<h3>Các văn bản liên quan bị ảnh hưởng sửa đổi</h3>\r\n" +
                                "<ul>\r\n";
                        for (int iIndex = 0; iIndex < ResCmsData.Count; iIndex++)
                        {
                            if (hs.ContainsKey(ResCmsData[iIndex].DataId + ResCmsData[iIndex].RelationTypeName) == false)
                            {
                                Html += " <li><a href=\"?gid=" + gid + "&DataId=" + ResCmsData[iIndex].DataId + "&GroupDataFolderId=" + GroupDataFolderId + "&ShowRelChanged=1\">" + ResCmsData[iIndex].Subject + "</a> - " + ResCmsData[iIndex].RelationTypeName + "</li>\r\n";
                                hs.Add(ResCmsData[iIndex].DataId + ResCmsData[iIndex].RelationTypeName, null);
                            }
                        }
                        Html += "</ul>\r\n" +
                            "</div>\r\n";
                    }
                }
                Html+=
                    " </div>";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddRelationDocument(
            string SiteId,
            string GroupDataId,
            string SrcDataId,
            string RelationTypeId,
            string documentNo,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(SiteId, documentNo);
                if (OCmsData == null)
                {
                    throw new Exception("Số văn bản liên quan không hợp lệ! Không tìm thấy số văn bản này!");
                }
                if (OCmsData.Equals(SrcDataId))
                {
                    throw new Exception("Số văn bản liên quan không hợp lệ! Văn bản liên quan không thể chính là văn bản này!");
                }
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddDocumentRelation(SiteId, SrcDataId, OCmsData.DataId, RelationTypeId);
                OAjaxOut.RetUrl = "ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId=" + SrcDataId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteRelationDocument(
            string SiteId,
            string RelationDataId,
            string GroupDataId,
            string SrcDataId,
            string RelationTypeId,
            string GroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).DeleteDocumentRelation(SiteId, RelationDataId);
                OAjaxOut.RetUrl = "?gid=" + GroupDataId + "&id=" + SrcDataId + "&GroupDataFolderId=" + GroupDataFolderId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteDocument(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId,
            string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).DeleteDocument(SiteId, DataId);
                OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid="+GroupDataId+"&GroupDataFolderId=" + GroupDataFolderId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideRemoveDocumentFromFolder(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId,
            string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).RemoveDocumentFromFolder(SiteId, GroupDataFolderId,DataId);
                OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }




        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateDocument(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string DataId = (string)WebEnvironments.Request(SiteId, "DataId");
                string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);
                if (OGroupData == null)
                {
                    throw new Exception("Tham số GroupData không hợp lệ!");
                }
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(SiteId, DataId);
                string StyleDisp = " style=\"display:none;\"";
                

                string Html =
                     "  <script language=javascript>\r\n" +
                     "        function ClientSaveDocument(GroupDataId,GroupDataFolderId,DataId)\r\n" +
                     "        {\r\n" +
                     "          SiteId='"+SiteId+"';\r\n"+
                     //"          DataId = document.getElementById('txtUpdateId').value;\r\n" +
                     "          Alias = document.getElementById('txtUpdateAlias').value;\r\n" +
                     "          Subject = document.getElementById('txtUpdateSubject').value;\r\n" +
                     "          Description = document.getElementById('txtUpdateDescription').value;\r\n" +
                     "          Body='';\r\n" +
                     "          if(FCKeditorAPI!=null){\r\n" +
                     "              oEditor = FCKeditorAPI.GetInstance('FCKeditorBodyUpdate');\r\n" +
                     "              Body= oEditor.GetXHTML( true ); \r\n" +
                     "          }\r\n" +
                     "          Source = document.getElementById('txtUpdateSource').value;\r\n" +
                     "          Url = document.getElementById('txtUpdateUrl').value;\r\n" +
                     "          Target = document.getElementById('txtUpdateTarget').value;\r\n" +
                     "          SortIndex = document.getElementById('txtUpdateSortIndex').value;\r\n" +
                     "          ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +

                     "      DocType='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectDocType')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocType=document.getElementById('drpUpdateSelectDocType').value;\r\n" +
                     "      }\r\n" +

                     "      DocField='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectDocField')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocField=document.getElementById('drpUpdateSelectDocField').value;\r\n" +
                     "      }\r\n" +

                     "      DocReleaseFrom='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectDocReleaseFrom')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocReleaseFrom=document.getElementById('drpUpdateSelectDocReleaseFrom').value;\r\n" +
                     "      }\r\n" +

                     "      DocSigner='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectDocSigner')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocSigner=document.getElementById('drpUpdateSelectDocSigner').value;\r\n" +
                     "      }\r\n" +


                     "      DocStatus='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectDocStatus')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocStatus=document.getElementById('drpUpdateSelectDocStatus').value;\r\n" +
                     "      }\r\n" +

                     "      ReleaseDate='';\r\n" +
                     "      if(document.getElementById('txtUpdateReleaseDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReleaseDate=document.getElementById('txtUpdateReleaseDate').value;\r\n" +
                     "      }\r\n" +

                     "      EffectedDate='';\r\n" +
                     "      if(document.getElementById('txtUpdateEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EffectedDate=document.getElementById('txtUpdateEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      EndEffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEndUpdateEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EndEffectedDate=document.getElementById('txtEndUpdateEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      SignDate='';\r\n" +
                     "      if(document.getElementById('txtUpdateSignDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          SignDate=document.getElementById('txtUpdateSignDate').value;\r\n" +
                     "      }\r\n" +

                     "      ReplaceDoc='';\r\n" +
                    //"      if(document.getElementById('txtUpdateReplaceDoc')!=null)\r\n" +
                    //"      {\r\n" +
                    //"          ReplaceDoc=document.getElementById('txtUpdateReplaceDoc').value;\r\n" +
                    //"      }\r\n" +

                     "      ModifyDoc='';\r\n" +
                    //"      if(document.getElementById('txtUpdateModifyDoc')!=null)\r\n" +
                    //"      {\r\n" +
                    //"          ModifyDoc=document.getElementById('txtUpdateModifyDoc').value;\r\n" +
                    //"      }\r\n" +


                     "      VoteType='';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectVoteType')!=null)\r\n" +
                     "      {\r\n" +
                     "          VoteType=document.getElementById('drpUpdateSelectVoteType').value;\r\n" +
                     "      }\r\n" +

                     "      IsNew=document.getElementById('drpSelectIsNew').value;\r\n" +

                     "      AttachedSearchable='0';\r\n" +
                     "      if(document.getElementById('drpUpdateSelectSearchable')!=null)\r\n" +
                     "      {\r\n" +
                     "          AttachedSearchable=document.getElementById('drpUpdateSelectSearchable').value;\r\n" +
                     "      }\r\n" +

                     "      AjaxOut = Portal.Cms.Render.DataRenderByTreeBll.ServerSideSave(SiteId,GroupDataId,GroupDataFolderId,ParentDataId,DataId, Alias, Subject, Description, Body, Source, Url, Target, SortIndex,DocType,DocField, DocReleaseFrom,DocSigner, DocStatus,SignDate, ReleaseDate,EffectedDate,EndEffectedDate,ReplaceDoc,ModifyDoc, VoteType, IsNew, AttachedSearchable).value;\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "  }\r\n" +

                        
                     "  </script>\r\n" +
                     " <div class=DatabaseFunction>\r\n" +
                       "        <ul>\r\n" +
                       "            <li onclick=\"javascript:ClientSaveDocument('"+GroupDataId+"','"+GroupDataFolderId+"','"+DataId+"');\"><img class=image src=\"Resources/Images/Save.png\"><div>Ghi lại</div></li>\r\n" +
                       "            <li onclick=\"javascript:window.open('ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId="+DataId+"','_self');\"><img class=image src=\"Resources/Images/Back.png\"><div>Quay lại</div></li>\r\n" +
                       "        </ul>\r\n" +
                       "  </div>\r\n" +

                   "  <input type=hidden id=txtParentDataId value=\""+OCmsData.ParentDataId+"\">\r\n" +
                   "  <input type=hidden id=txtUpdateId value=\"" + OCmsData.DataId + "\">\r\n" +
                   "  <h3>Sửa dữ liệu</h3>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n"+
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Mã:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateAlias class=textbox type=textbox value=\""+OCmsData.DataAlias+"\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Tiêu đề:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateSubject class=textbox type=textbox value=\""+OCmsData.Subject+"\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1 style=\"vertical-align:top;\">Mô tả:</td>\r\n" +
                   "        <td class=c2><textarea id=txtUpdateDescription class=textarea type=textbox>"+OCmsData.Description+"</textarea></td>\r\n" +
                   "    </tr>\r\n" +
                     "   <tr " + StyleDisp + ">\r\n" +
                     "        <td class=c1 style=\"vertical-align:top;\">Nội dung:</td>\r\n" +
                     "        <td class=c2>\r\n" +
                     "               <textarea type=hidden id=\"FCKeditorBodyUpdate\" name=\"FCKeditorBodyUpdate\">"+OCmsData.Body+"</textarea>\r\n" +
                     "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                     "               <script type=\"text/javascript\">\r\n" +
                     "                   var oFCKeditorUpdate = new FCKeditor('FCKeditorBodyUpdate');\r\n" +
                     "                   oFCKeditorUpdate.BasePath = \"/fckeditor/\";\r\n" +
                     "                   oFCKeditorUpdate.Height = \"600\";\r\n" +
                     "                   oFCKeditorUpdate.ReplaceTextarea();\r\n" +
                     "               </script>\r\n" +
                     "        </td>\r\n" +
                     "    </tr>\r\n";

                string SelectIsNew = " <select id=\"drpSelectIsNew\">\r\n" +
                                     "      <option value=\"0\">Tài liệu bình thường</option>\r\n" +
                                     "      <option value=\"1\">Tài liệu mới</option>\r\n" +
                                     " </select>\r\n";

                if (OCmsData.IsNew==1)
                {
                    SelectIsNew = " <select id=\"drpSelectIsNew\">\r\n" +
                                     "      <option value=\"0\">Tài liệu bình thường</option>\r\n" +
                                     "      <option selected value=\"1\">Tài liệu mới</option>\r\n" +
                                     " </select>\r\n";
                }
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    string SelectSearchable =
                        " <select id=\"drpUpdateSelectSearchable\">\r\n" +
                        "      <option value=\"0\">Không tìm kiếm toàn văn được</option>\r\n" +
                        "      <option value=\"1\" selected>Tìm kiếm toàn văn được</option>\r\n" +
                        " </select>\r\n";
                    if (OCmsData.AttachedSearchable==1)
                    {
                        SelectSearchable =
                        " <select id=\"drpUpdateSelectSearchable\">\r\n" +
                        "      <option value=\"0\">Không tìm kiếm toàn văn được</option>\r\n" +
                        "      <option selected value=\"1\" selected>Tìm kiếm toàn văn được</option>\r\n" +
                        " </select>\r\n";
                    }
                    string SelectDocumentType =
                        "<select id=drpUpdateSelectDocType class=drplist>\r\n";
                    DocTypeCls[] DocTypes = DocTypeProcessBll.ReadingDocType(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
                    {
                        if (OCmsData.frkDocTypeId.Equals(DocTypes[iIndex].DocTypeId))
                        {
                            SelectDocumentType += "   <option selected value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                        }
                        else
                        {
                            SelectDocumentType += "   <option value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                        }
                    }
                    SelectDocumentType += "</select>\r\n";


                    string SelectDocField =
                       "<select id=drpUpdateSelectDocField class=drplist>\r\n";
                    DocFieldCls[] DocFields = DocFieldProcessBll.ReadingDocField(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
                    {
                        if (OCmsData.frkDocFieldId.Equals(DocFields[iIndex].DocFieldId))
                        {
                            SelectDocField += "   <option selected value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                        }
                        else
                        {
                            SelectDocField += "   <option value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                        }
                    }
                    SelectDocField += "</select>\r\n";

                    string SelectDocReleaseFrom =
                        "<select id=drpUpdateSelectDocReleaseFrom class=drplist>\r\n";
                    DocReleaseFromCls[] DocReleaseFroms = DocReleaseFromProcessBll.ReadingDocReleaseFrom(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocReleaseFroms.Length; iIndex++)
                    {
                        if (DocReleaseFroms[iIndex].DocReleaseFromId.Equals(OCmsData.frkDocReleaseFromId))
                        {
                            SelectDocReleaseFrom += "   <option selected value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                        }
                        else
                        {
                            SelectDocReleaseFrom += "   <option value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                        }
                    }
                    SelectDocReleaseFrom += "</select>\r\n";

                    string SelectDocSigner =
                      "<select id=drpUpdateSelectDocSigner class=drplist>\r\n";
                    DocSignerCls[] DocSigners = DocSignerProcessBll.ReadingDocSigner(SiteId, GroupDataId);
                    for (int iIndex = 0; iIndex < DocSigners.Length; iIndex++)
                    {
                        if (DocSigners[iIndex].DocSignerId.Equals(OCmsData.frkDocSignerId))
                        {
                            SelectDocSigner += "   <option selected value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                        }
                        else
                        {
                            SelectDocSigner += "   <option value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                        }
                    }
                    SelectDocSigner += "</select>\r\n";



                    string SelectStatus =
                        "<select id=drpUpdateSelectDocStatus class=drplist>\r\n" +
                        "   <option value=\"0\">Hết hiệu lực</option>\r\n" +
                        "   <option value=\"1\">Có hiệu lực</option>\r\n" +
                        "</select>\r\n";
                    
                    Html +=
                     "    <tr>\r\n" +
                     "        <td class=c1>Loại văn bản:</td>\r\n" +
                     "        <td class=c2>" + SelectDocumentType + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Lĩnh vực:</td>\r\n" +
                     "        <td class=c2>" + SelectDocField + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Nơi ban hành:</td>\r\n" +
                     "        <td class=c2>" + SelectDocReleaseFrom + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Người ký:</td>\r\n" +
                     "        <td class=c2>" + SelectDocSigner + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Trạng thái:</td>\r\n" +
                     "        <td class=c2>" + SelectStatus + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày ký:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtUpdateSignDate", OCmsData.SignDate) + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày ban hành:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtUpdateReleaseDate", OCmsData.ReleaseDate) + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày hiệu lực:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtUpdateEffectedDate", OCmsData.EffectedDate) + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Ngày hết hiệu lực:</td>\r\n" +
                     "        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtEndUpdateEffectedDate", OCmsData.EndEffectedDate) + "</td>\r\n" +
                     "    </tr>\r\n" +
                     "    <tr>\r\n" +
                     "        <td class=c1>Tìm toàn văn:</td>\r\n" +
                     "        <td class=c2>" + SelectSearchable + "</td>\r\n" +
                     "    </tr>\r\n";
                }

                Html +=

                   "    <tr>\r\n" +
                   "        <td class=c1>Nguồn:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateSource class=textbox type=textbox value=\"" + OCmsData.Source + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Url:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateUrl class=textbox type=textbox value=\"" + OCmsData.Url + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Kiểu mở:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateTarget class=textbox type=textbox value=\"" + OCmsData.Target + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Thứ tự:</td>\r\n" +
                   "        <td class=c2><input id=txtUpdateSortIndex class=textbox type=textbox value=\"" + OCmsData.SortIndex + "\"></td>\r\n" +
                   "    </tr>\r\n" +

                   "    <tr>\r\n" +
                   "        <td class=c1>Trạng thái:</td>\r\n" +
                   "        <td class=c2>" + SelectIsNew + "</td>\r\n" +
                   "    </tr>\r\n" +
                   //"    <tr>\r\n" +
                   //"        <td class=c1></td>\r\n" +
                   //"        <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:SaveUpdate();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                   //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";


                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(
            string SiteId,
            string GroupDataId,
            string GroupDataFolderId,
            string ParentDataId,
            string DataId,
            string Alias,
            string Subject,
            string Description,
            string Body,
            string Source,
            string Url,
            string Target,
            string SortIndex,
            string DocType,
            string DocField,
            string DocReleaseFrom,
            string DocSigner,
            string DocStatus,
            string SignDate,
            string ReleaseDate,
            string EffectedDate,
            string EndEffectedDate,
            string ReplaceDoc,
            string ModifyDoc,
            string VoteType,
            int IsNew,
            int AttachedSearchable)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề dữ liệu!");
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    if (FunctionUtilities.checkVnDate(ReleaseDate) == false) throw new Exception("Ngày ban hành không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EffectedDate) == false) throw new Exception("Ngày hiệu lực không hợp lệ!");
                }
                DateTime dReleaseDate = new System.DateTime();
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    dReleaseDate = FunctionUtilities.VNDateToDate(ReleaseDate);
                }
                DateTime dEffectedDate = new System.DateTime();
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    dEffectedDate = FunctionUtilities.VNDateToDate(EffectedDate);
                }

                DateTime dEndEffectedDate = new System.DateTime();
                if (!string.IsNullOrEmpty(EndEffectedDate))
                {
                    dEndEffectedDate = FunctionUtilities.VNDateToDate(EndEffectedDate);
                }
                DateTime dSignDate = new System.DateTime();
                if (!string.IsNullOrEmpty(SignDate))
                {
                    dSignDate = FunctionUtilities.VNDateToDate(SignDate);
                }
                GroupDataCls OGroupDataCls = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(SiteId, GroupDataId);

                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).SaveCmsData(SiteId, DataId, Alias, Subject, Description, Body, Source, Url, Target, SortIndex, DocType, DocField, DocReleaseFrom, DocSigner, DocStatus, dSignDate, dReleaseDate, dEffectedDate, dEndEffectedDate, ReplaceDoc, ModifyDoc, VoteType, true, IsNew, AttachedSearchable);
                OAjaxOut.RetUrl = "ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataId + "&DataId=" + DataId+"&ShowRelChanged=1";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateFolder(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
            string pgid = (string)WebEnvironments.Request(SiteId, "pgid");
            GroupDataFolderCls OGroupDataFolder = GroupDataFolderProcessBll.CreateGroupDataFolderModel(SiteId, pgid);
            try
            {
                string Html =
                    "<script language=javascript>\r\n" +
                    "   function ClientSaveFolder(GroupDataId,pgid)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       FolderName = document.getElementById('txtFolderName').value;\r\n" +
                    "       FolderDescription = document.getElementById('txtFolderDescription').value;\r\n" +
                    "       SortIndex= document.getElementById('txtSortIndex').value;\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.DataRenderByTreeBll.ServerSideSaveFolder(SiteId,pgid,FolderName,FolderDescription, SortIndex).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(OAjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open('DocumentInputDataTreeView.aspx?gid='+GroupDataId,'_self');\r\n" +
                    "   }\r\n" +
                    "</script>\r\n" +
                    "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSaveFolder('" + GroupDataId + "','" + pgid + "');\"><img class=image src=\"Resources/Images/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('Collection.aspx?gid=" + GroupDataId + "','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n" +
                    "<div class=CssTip>Cho phép người quản lý tạo ra các thư mục dữ liệu. Các thư mục này sẽ chứa các văn bản tài liệu và phân quyền cho các đối tượng truy cập khai thác</div>\r\n" +
                    "<div class=CssFormData>\r\n" +
                    "   <table class=tabMain>\r\n" +
                    "       <tr>\r\n" +
                    "           <td><img src=\"Resources/Images/AddFolderLarge.png\"></td>\r\n" +
                    "           <td>\r\n" +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Tên thư mục:</td>\r\n" +
                    "       <td class=col2><input class=textbox type=textbox id=txtFolderName value=\"" + OGroupDataFolder.FolderName + "\"></td>\r\n" +
                    "   </tr>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Mô tả:</td>\r\n" +
                    "       <td class=col2><textarea id=txtFolderDescription type=textarea class=textarea>" + OGroupDataFolder.Description + "</textarea></td>\r\n" +
                    "   </tr>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1>Thứ tự:</td>\r\n" +
                    "       <td class=col2><input class=textbox type=textbox id=txtSortIndex value=\"" + OGroupDataFolder.SortIndex + "\"></td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</td>\r\n" +
                    "</tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveFolder(
            string SiteId,
            string GroupDataFolderId,
            string FolderName,
            string Description,
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataFolderProcessBll.SaveGroupDataFolder(SiteId, GroupDataFolderId, FolderName, Description, SortIndex);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }




    }
}
